#include "memMatch.h"
#include "memPlayer.h"
#include "MAHeaders.h"
#include "Util.h"
#include <MAUI/ListBox.h>
#include <MAUI/Image.h>
#include <madmath.h>
#include <mastdlib.h>
#include <conprint.h>
#include <mavsprintf.h>
#include <MAUI/Engine.h>
#include <MAUI/Font.h>
#include <MAUI/Label.h>
#include <MAUI/EditBox.h>
#include <MAUtil/Moblet.h>
#include <ma.h>
#include <mavsprintf.h>
#include <IX_WIDGET.h>

using namespace MAUtil;
using namespace MAUI;


memMatch::memMatch(Screen *previous, User *myUser) : previous(previous), myUser(myUser) {

	score = 0;
	mainLayout = createMainLayout("NEW", "Back");
	this->setMain(mainLayout);
	ListBox* listBox = (ListBox*) mainLayout->getChildren()[0];
	layout = new Layout(0, 0, scrWidth, scrHeight-mainLayout->getChildren()[1]->getHeight(), listBox, 4, 4);
	layout->setMarginX(5);
	layout->setMarginY(5);
	layout->setPaddingLeft(5);
	layout->setPaddingRight(5);
	layout->setDrawBackground(false);

	myPlayer = new memPlayer();

	for(int i = 0; i < 4; i++) {
		for(int j = 0; j < 4; j++){
			myImage[i][j] = new Image(0, 0, 0, 0, layout, true, true, CARD_DOWN);
		}
	}

	turn = 0;
	card1 = -1;
	card2 = -1;
}

memMatch::~memMatch() {


}

void memMatch::keyPressEvent(int keyCode, int nativeCode) {

	if(keyCode == MAK_SOFTRIGHT) {
		previous->show();
	}

	if(keyCode == MAK_SOFTLEFT){
		for(int i = 0; i < 4; i++){
			for(int j = 0; j < 4; j++){
				myImage[i][j]->setResource(CARD_DOWN);
			}
		}
		myPlayer = new memPlayer();
	}

	if(keyCode == MAK_1)
	{
		keyPressEvent(MAK_SOFTLEFT, 0);
	}
	if(keyCode == MAK_2)
	{
		previous->show();
	}
}

void memMatch::pointerPressEvent(MAPoint2d point) {
	Point p;
	p.set(point.x, point.y);
	int cardVal = -1;

	for(int i = 0; i < 4; i++){
		for(int j = 0; j < 4; j++)
		{
			if(myImage[i][j]->contains(p))
			{
				if(turn == 2){
					if(card1 != card2)	// check if the previous cards were equal
					{
						myImage[firstCardi][firstCardj]->setResource(CARD_DOWN);
						myImage[secondCardi][secondCardj]->setResource(CARD_DOWN);
						score += -10;
					}
					else{
						score += 10;
					}
					turn = 0;
				}

				if(turn == 0){
					card1 = myPlayer->getCard(i, j);
					firstCardi = i;
					firstCardj = j;
				}

				if(turn == 1){
					card2 = myPlayer->getCard(i, j);
					secondCardi = i;
					secondCardj = j;
				}

				cardVal = myPlayer->getCard(i, j);
				lprintfln("Card Value %cardVal", cardVal);
				if(cardVal < 0 || cardVal > 7)
					myImage[i][j]->setResource(ACE_SPADES);
				if(cardVal == 0)
					myImage[i][j]->setResource(FACEONE);
				if(cardVal == 1)
					myImage[i][j]->setResource(FACETWO);
				if(cardVal == 2)
					myImage[i][j]->setResource(FACETHREE);
				if(cardVal == 3)
					myImage[i][j]->setResource(FACEFOUR);
				if(cardVal == 4)
					myImage[i][j]->setResource(FACEFIVE);
				if(cardVal == 5)
					myImage[i][j]->setResource(FACESIX);
				if(cardVal == 6)
					myImage[i][j]->setResource(FACESEVEN);
				if(cardVal == 7)
					myImage[i][j]->setResource(FACEEIGHT);
				turn++;
			}
		}
	}


	if(softKeys->contains(p)) {
		if(softKeys->getChildren()[0]->contains(p)) {
			keyPressEvent(MAK_SOFTLEFT, 0);
		}
		else if(softKeys->getChildren()[1]->contains(p)) {
			keyPressEvent(MAK_SOFTRIGHT, 0);
		}
	}

}

void memMatch::pointerReleaseEvent(MAPoint2d point) {

}
